﻿using System;
using Google.Analytics.Model.Feeds;
using Google.GData.Client;

namespace Google.Analytics.RemoteAccess
{
    public class AnalyticsService : Service
    {
        public const string GAnalyticsService = "analytics";

        /// <summary>
        /// Creates new instance of the <see cref="AnalyticsService"/>
        /// </summary>
        /// <returns></returns>
        public AnalyticsService(string applicationName) : base(GAnalyticsService, applicationName)
        {
            NewFeed += OnNewFeed;
        }

        static void OnNewFeed(object sender, ServiceEventArgs e)
        {
            Tracing.TraceMsg("Created new Analytics Feed");
            if (e == null)
            {
                throw new ArgumentNullException("e");
            }
            if (e.Uri.AbsoluteUri.IndexOf("/acl/") != -1)
            {
                e.Feed = new AtomFeed(e.Uri, e.Service);
            }
            else if (e.Uri.AbsoluteUri.IndexOf("/accounts/") != -1)
            {
                e.Feed = new AccountFeed(e.Uri, e.Service);
            }
            else
            {
                e.Feed = new DataFeed(e.Uri, e.Service);
            }
        }
    }
}
